import Vue from "vue";
import Vuex from "vuex";
import { nanoid } from "nanoid";
Vue.use(Vuex);
const store = new Vuex.Store({
  // strict: true,
  state: {
    list: [],
    isSel: "all",
  },
  mutations: {
    // 添加事件
    addList(state, info) {
      let i = { id: nanoid(), name: info, isDone: false };
      state.list.push(i);
    },
    // 删除事件
    removeList(state, id) {
      state.list = state.list.filter((item) => {
        return item.id !== id;
      });
    },
    // 过滤数据
    filterList(state, isSel) {
      state.isSel = isSel;
    },
    // 清除全部一完成
    clearDone(state) {
      state.list = state.list.filter((item) => {
        return item.isDone == false;
      });
    },
    chooseAll(state, m) {
      state.list.forEach((item) => {
        item.isDone = m;
      });
    },
  },
  actions: {},
  getters: {
    //   过滤数据
    showList(state) {
      if (state.isSel == "all") {
        return state.list;
      } else if (state.isSel == "no") {
        return state.list.filter((item) => {
          return !item.isDone;
        });
      } else {
        return state.list.filter((item) => {
          return item.isDone;
        });
      }
    },
  },
  modules: {},
});
export default store;
